package org.dromara.system.controller.system;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.vo.SysLabelRequestVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysLabelVo;
import org.dromara.system.domain.bo.SysLabelBo;
import org.dromara.system.service.ISysLabelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 系统管理-门店标签配置
 * 前端访问路由地址为:/system/label
 *
 * @author LionLi
 * @date 2024-03-18
 */
@Validated
@RequiredArgsConstructor
@RestController
    @RequestMapping("/label")
public class SysLabelController extends BaseController {

    private final ISysLabelService sysLabelService;

    /**
     * 查询系统管理-门店标签配置列表
     */
    @SaCheckPermission("system:label:list")
    @GetMapping("/list")
    public List<SysLabelVo> list() {
        return sysLabelService.queryList();
    }



    /**
     * 新增系统管理-门店标签配置
     */
    @SaCheckPermission("system:label:add")
    @Log(title = "系统管理-门店标签配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysLabelRequestVo sysLabelRequestVo) {
        List<SysLabelBo> tagInfoList = sysLabelRequestVo.getTagInfoList();
        //校验门店标签是否重复
        for (SysLabelBo bo : tagInfoList) {
            if (!sysLabelService.checkLabeName(bo)) {
                return R.fail("添加参数'" + bo.getLabelName()+ "'失败，门店标签已存在");
            }

        }
        return toAjax(sysLabelService.insertByBo(tagInfoList.get(0)));
    }

    /**
     * 修改系统管理-门店标签配置
     */
    @SaCheckPermission("system:label:edit")
    @Log(title = "系统管理-门店标签配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysLabelRequestVo sysLabelRequestVo) {
        List<SysLabelBo> tagInfoList = sysLabelRequestVo.getTagInfoList();

        for (SysLabelBo bo : tagInfoList) {
            if (!sysLabelService.checkLabeName(bo)) {
                return R.fail("修改参数'" + bo.getLabelName()+ "'失败，门店标签已存在");
            }
        }
        return toAjax(sysLabelService.updateByBo(tagInfoList));
    }

    /**
     * 删除系统管理-门店标签配置
     *
     * @param vo 主键串
     */
    @SaCheckPermission("system:label:remove")
    @Log(title = "系统管理-门店标签配置", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody SysLabelRequestVo vo) {
        List<String> ids = vo.getIds();
        return toAjax(sysLabelService.deleteWithValidByIds(ids));
    }
}
